package com.haochang.base;

import java.util.LinkedList;
import java.util.List;

import com.haochang.base.utils.WakeLockUtil;
import com.haochang.chunkvod.app.utils.CollectionUtils;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;

/**
 * @author lxl
 *         2015-7-3-下午12:50:40
 *         <p/>
 *         Activity管理基类
 *         <p/>
 *         目的性:鉴于目前Activity ActivityGroup FragmentActivity 不能统一用Activity管理的情况,
 * @update qintao
 * 新增activity管理，统一关闭所有存在的activity
 */
public class BaseActivityManager
{
	private static BaseActivityManager instance = null;
	private List<Activity> mActivityList = new LinkedList<Activity>();

	public static BaseActivityManager getInstance()
	{
		if (instance == null)
		{
			synchronized (BaseActivityManager.class)
			{
				if (instance == null)
					instance = new BaseActivityManager();
			}
		}
		return instance;
	}

	protected void onPause(Context context)
	{
		WakeLockUtil.release();
	}

	protected void onResume(Context context)
	{
		WakeLockUtil.acquire(context);

		//XXX imageloader bug
		//设置 PauseOnScrollListener 引起的imageLoader.pause()没有调用imageLoader.resume()问题解决方案
		//        ImageLoader.getInstance().resume();
	}

	protected void onSaveInstanceState(Bundle outState)
	{

	}

	public void addActivity(Activity activity)
	{
		if (CollectionUtils.isEmpty(mActivityList))
			mActivityList = new LinkedList<Activity>();
		if (!mActivityList.contains(activity))
			mActivityList.add(activity);
	}

	/**
	 * 关闭所有的activity
	 */
	public void closeAllActivity()
	{
		try
		{
			if (CollectionUtils.isEmpty(mActivityList))
				return;
			for (Activity activity : mActivityList)
			{
				if (activity != null && !activity.isFinishing())
				{
					activity.finish();
				}
			}
			mActivityList.clear();
		} catch (Exception e)
		{
			e.printStackTrace();
		}
	}
}
